package com.gzone.contentprovider.sql.dao;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.database.Cursor;

import com.gzone.contentprovider.helper.DataHelper;
import com.gzone.contentprovider.sql.model.Hit;
import com.gzone.contentprovider.sql.model.Item;
import com.gzone.contentprovider.sql.model.Selection;

public class SelectionDAO implements DAO<Selection>{
	private static final String TABLE_NAME ="Selection";
	private static final String ID ="Selection_ID";
	private static final String CAT_ID ="Selection_cat_ID";
	private static final String ITEM_ID ="Selection_item_ID";
	private static final String COUNT ="Selection_count";
	public SelectionDAO(){
		DataHelper.dropTable(TABLE_NAME);
		String tableSql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ("
		+ ID + " INTEGER PRIMARY KEY AUTOINCREMENT," 
		+ CAT_ID + " INTEGER,"
		+ ITEM_ID + " INTEGER,"
		+ COUNT + " INTEGER"
		+");";
		DataHelper.createTable(tableSql);
	}
	@Override
	public void delete(Selection object) {
		DataHelper.delete(TABLE_NAME, object.id);
	}
	
	@Override
	public void deleteAllRow() {
		DataHelper.deleteAllRow(TABLE_NAME);
	}

	@Override
	public ArrayList<Selection> findAll() {
		Cursor data = DataHelper.readAll(TABLE_NAME);
		ArrayList<Selection> list = new ArrayList<Selection>();
		if (data.getCount() > 0) {
			data.moveToFirst();
			int id = data.getColumnIndex(ID);
			int cat_id = data.getColumnIndex(CAT_ID);
			int item_id = data.getColumnIndex(ITEM_ID);
			int cound = data.getColumnIndex(COUNT);
			do {
				Selection obj = new Selection();
				obj.id = data.getInt(id);
				obj.cat_id = data.getInt(cat_id);
				obj.item_id = data.getInt(item_id);
				obj.count = data.getInt(cound);
				list.add(obj);
			} while (data.moveToNext());
		}
		return list;
	}

	@Override
	public Selection findById(int id) {
		Cursor data = DataHelper.findByParam(TABLE_NAME, ID, id);
		Selection obj = null;
		if (data.getCount() > 0) {
			data.moveToFirst();
			int sid = data.getColumnIndex(ID);
			int cat_id = data.getColumnIndex(CAT_ID);
			int item_id = data.getColumnIndex(ITEM_ID);
			int cound = data.getColumnIndex(COUNT);
			obj = new Selection();
			obj.id = data.getInt(sid);
			obj.cat_id = data.getInt(cat_id);
			obj.item_id = data.getInt(item_id);
			obj.count = data.getInt(cound);
		}
		return obj;
	}

	@Override
	public ArrayList<Selection> findByParameter(String paraName, Object value) {
		Cursor data = DataHelper.findByParam(TABLE_NAME, paraName, value);
		ArrayList<Selection> list = new ArrayList<Selection>();
		if (data.getCount() > 0) {
			data.moveToFirst();
			int id = data.getColumnIndex(ID);
			int cat_id = data.getColumnIndex(CAT_ID);
			int item_id = data.getColumnIndex(ITEM_ID);
			int cound = data.getColumnIndex(COUNT);
			do {
				Selection obj = new Selection();
				obj.id = data.getInt(id);
				obj.cat_id = data.getInt(cat_id);
				obj.item_id = data.getInt(item_id);
				obj.count = data.getInt(cound);
				list.add(obj);
			} while (data.moveToNext());
		}
		return list;
	}

	@Override
	public void insert(Selection object) {
		// TODO Auto-generated method stub
		String insert = "INSERT INTO '" + TABLE_NAME + "' ("
		+ CAT_ID + ","
		+ ITEM_ID + ","
		+ COUNT + ""
		+") VALUES(?,?,?);";
		Object[] bindArgs = new Object[]{ object.cat_id, object.item_id, object.count};
		DataHelper.insert(insert, bindArgs );
	}

	@Override
	public void update(Selection object) {
		String update = "UPDATE '" + TABLE_NAME + "' SET "
		+ CAT_ID + "=?,"
		+ ITEM_ID + "=?,"
		+ COUNT + "=?"
		+" WHERE "+ ID + "=?;" ;
		Object[] bindArgs = new Object[]{ object.cat_id, object.item_id, object.count,object.id};
		DataHelper.update(update, bindArgs );
		
	}
	@Override
	public ArrayList<Selection> findAllToUpdate() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public ArrayList<Selection> readJSONData(String data) {
		return null;
	}

}
